<?php

/**
 * Manages your dynamic/stored params,generates sql,processes params in url
 *
 * @author Hasnain Khan <hasnain.khan@newgenbd.net>
 * @package product
 * @subpackage libraries
 */
class search_processor_l_product {
    const MAX = 1000;
    private $product_name;
    private $upc;

    public function __construct($product_name, $upc) {
        $this->product_name = $product_name;
        $this->upc = $upc;
    }

    public function list_product($offset, $num_of_records, $column, $order, $filters)
    {
        $list = array();
        $table = array(
            0 => "id",
            1 => "short_desc",
            2 => "upc",
            3 => "price",
            4 => "publish",
            5 => "category_id",
            6 => "title"
        );

        $query = $this->search_product($offset, $num_of_records, $table[($column+1)], $order, $filters);

        foreach($query as $row){
            $action = "<a href='".base_url()."index.php/product/admin/add_edit_product/view_product/".$row->$table[0]."/".$row->$table[5]."'>
                                <img src='".base_url()."application/modules/core/images/common/detail.png' title='Details'  />
                           </a> &nbsp;&nbsp;&nbsp;&nbsp";
            $action = $action."<a href='".base_url()."index.php/product/admin/add_edit_product/edit_product/".$row->$table[0]."/".$row->$table[5]."'>
                                <img src='".base_url()."application/modules/core/images/common/edit.png' title='Edit'  />
                           </a> &nbsp;&nbsp;&nbsp;&nbsp";
            $action = $action."<a href='".base_url()."index.php/product/admin/add_edit_product/delete_product/".$row->$table[0]."'>
                                <img src='".base_url()."application/modules/core/images/common/delete.png' title='Delete'  />
                           </a>";

            $list[] = array($row->$table[1], $row->$table[2], $row->$table[3], $row->$table[4], $row->$table[6], $action);
        }

        return $list;
    }

    public function search_product($offset=0, $num_of_records=self::MAX, $column="short_desc", $order="ASC", $filter="")
    {
        $is_deleted =0;
        $p = new product_m_product();

        $p->query('   SELECT products.id, languages_products.short_desc, products.upc, products.price, products.publish, categories_products.category_id, languages_categories.title
                        FROM products
                        INNER JOIN languages_products
                        ON products.id = languages_products.product_id
                        INNER JOIN categories_products
                        ON products.id = categories_products.product_id
                        INNER JOIN languages_categories
                        ON languages_categories.category_id = categories_products.category_id
                        WHERE ( products.is_deleted = \''.$is_deleted.'%\' AND languages_products.short_desc LIKE \'%'.$this->product_name.'%\' AND products.upc LIKE \'%'.$this->upc.'%\')
                                AND
                              ( languages_products.short_desc LIKE \'%'.$filter.'%\' OR products.upc LIKE \'%'.$filter.'%\')

                        ORDER BY '.$column.' '.$order.'
                        LIMIT '.$offset.', '.$num_of_records.' ');

        return $p;
    }

    public function count_products()
    {
        $count = 0;
        $search_count = new search_processor_l_product($this->product_name, $this->upc);
        $query = $search_count->search_product();

        foreach($query as $row){
            $count++;
        }

        return $count;
    }
}

?>